package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduKnowledgePointMapper;
import com.zenith.edu.po.domain.EduKnowledgePoint;
import com.zenith.edu.po.dto.EduKnowledgePointDTO;
import com.zenith.edu.po.vo.EduKnowledgePointVO;
import com.zenith.edu.service.EduKnowledgePointService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduKnowledgePointServiceImpl extends ServiceImpl<EduKnowledgePointMapper, EduKnowledgePoint> implements EduKnowledgePointService {
    
    @Override
    public EduKnowledgePointVO createKnowledgePoint(EduKnowledgePointDTO dto) {
        EduKnowledgePoint knowledgePoint = new EduKnowledgePoint();
        BeanUtils.copyProperties(dto, knowledgePoint);
        this.save(knowledgePoint);
        
        EduKnowledgePointVO vo = new EduKnowledgePointVO();
        BeanUtils.copyProperties(knowledgePoint, vo);
        return vo;
    }
    
    @Override
    public EduKnowledgePointVO getKnowledgePointById(Long id) {
        EduKnowledgePoint knowledgePoint = this.getById(id);
        if (knowledgePoint == null) {
            return null;
        }
        
        EduKnowledgePointVO vo = new EduKnowledgePointVO();
        BeanUtils.copyProperties(knowledgePoint, vo);
        return vo;
    }
    
    @Override
    public EduKnowledgePointVO updateKnowledgePoint(EduKnowledgePointDTO dto) {
        EduKnowledgePoint knowledgePoint = new EduKnowledgePoint();
        BeanUtils.copyProperties(dto, knowledgePoint);
        this.updateById(knowledgePoint);
        
        EduKnowledgePointVO vo = new EduKnowledgePointVO();
        BeanUtils.copyProperties(knowledgePoint, vo);
        return vo;
    }
    
    @Override
    public boolean deleteKnowledgePoint(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduKnowledgePointVO> getKnowledgePointList() {
        List<EduKnowledgePoint> knowledgePointList = this.list();
        return knowledgePointList.stream().map(knowledgePoint -> {
            EduKnowledgePointVO vo = new EduKnowledgePointVO();
            BeanUtils.copyProperties(knowledgePoint, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}